package com.iwhalecloud.bss.kite.cucc.service.attr;

import java.util.List;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IIndustryService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.KiteIndustryConfig;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.SubIndustryCache;

/**
 * Created by huanghaosheng on 2020/11/12 22:39
 */
@Service
public class IndustryService implements IIndustryService {

    @Override
    public List<KiteIndustryConfig> listSubIndustry(String parentCode) {
        return KiteStringUtils.isEmpty(parentCode) ?
            addLeafInfo(SubIndustryCache.get(SubIndustryCache.ROOT_INDUSTRY_KEY)) :
            addLeafInfo(SubIndustryCache.get(parentCode));
    }

    private List<KiteIndustryConfig> addLeafInfo(List<KiteIndustryConfig> kiteIndustryConfigList) {
        kiteIndustryConfigList.forEach(
            kiteIndustryConfig -> kiteIndustryConfig.setLeaf(isLeafIndustry(kiteIndustryConfig.getIndustryCode())));
        return kiteIndustryConfigList;
    }

    @Override
    public boolean isLeafIndustry(String parentCode) {
        return KiteListUtils.isEmpty(SubIndustryCache.get(parentCode));
    }
}
